﻿using NavigationDemo.Helpers;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;

namespace NavigationDemo.ViewModels
{
    public class SharedViewModel : BindableBase
    {
        string m_Title = "Invalid Title";
        public string Title
        {
            get { return m_Title; }
            set { SetProperty(ref m_Title, value); }
        }

        public class HistoryItem
        {
            public string Index { get; set; }
            public string Value { get; set; }
        }

        Page m_Page = default(Page);
        public Page Page
        {
            get { return m_Page; }
            set
            {
                SetProperty(ref m_Page, value);
                Windows.Storage.ApplicationData.Current.LocalSettings.Values[value.ToString()] = this.Title;
                var _State = value.Frame.GetNavigationState();
                var _Pages = _State.Split(',').Where(x => x.Contains("."));
                var _Current = int.Parse(_State.Split(',')[2].ToString());
                var _Total = int.Parse(_State.Split(',')[1].ToString());
                this.History.Clear();
                var _History = _Pages.Select((x, i) => new { Index = i, Value = x });
                foreach (var item in _History)
                {
                    var _Index = item.Index;
                    var _NewIndex = string.Empty;

                    if (_Index == _Current)
                        _NewIndex = "CURRENT";
                    else if (_Current < _Index)
                        _NewIndex = " + " + (_Total - item.Index).ToString();
                    else if (_Current > _Index)
                        _NewIndex = " - " + Math.Abs(_Current - item.Index).ToString();

                    var _Item = new HistoryItem
                    {
                        Index = _NewIndex,
                        Value = item.Value.Replace("NavigationDemo.Views.", string.Empty),
                    };

                    if (_Index == _Current)
                        this.History.Add(_Item);
                    else if (_Index < _Current)
                        this.History.Add(_Item);
                    else if (_Index > _Current)
                        this.History.Insert(_Current + 1, _Item);
                }
            }
        }

        ObservableCollection<HistoryItem> m_History = new ObservableCollection<HistoryItem>();
        public ObservableCollection<HistoryItem> History { get { return m_History; } set { SetProperty(ref m_History, value); } }

        #region GoBack DelegateCommand

        DelegateCommand m_GoBackCommand = null;
        public DelegateCommand GoBackCommand
        {
            get
            {
                if (m_GoBackCommand != null)
                    return m_GoBackCommand;
                m_GoBackCommand = new DelegateCommand(
                    GoBackCommandExecute, GoBackCommandCanExecute);
                this.PropertyChanged += (s, e) => m_GoBackCommand.RaiseCanExecuteChanged();
                return m_GoBackCommand;
            }
        }
        void GoBackCommandExecute() { Page.Frame.GoBack(); }
        bool GoBackCommandCanExecute() { if (Page == null) return false; return Page.Frame.CanGoBack; }

        #endregion

        #region GoForward DelegateCommand

        DelegateCommand m_GoForwardCommand = null;
        public DelegateCommand GoForwardCommand
        {
            get
            {
                if (m_GoForwardCommand != null)
                    return m_GoForwardCommand;
                m_GoForwardCommand = new DelegateCommand(
                    GoForwardCommandExecute, GoForwardCommandCanExecute);
                this.PropertyChanged += (s, e) => m_GoForwardCommand.RaiseCanExecuteChanged();
                return m_GoForwardCommand;
            }
        }
        void GoForwardCommandExecute() { Page.Frame.GoForward(); }
        bool GoForwardCommandCanExecute() { if (Page == null) return false; return Page.Frame.CanGoForward; }

        #endregion
    }
}
